<?php
/**
 * 门店卡券
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:39
 */
namespace Model;

class StoreCouponMod extends Mod
{
	public function __construct()
	{
		$this->_table = 'kuyuan_vbz_store_coupon';
	}

    /**
     * 获取门店卡券正在投放中的数量
     * @param $_coupon_id 门店卡券id
     * @param $_filed  查询综合的字段
     * @return int
     */
    public function putNumber($_coupon_id,$_filed)
    {
        $_sql = "SELECT 
						SUM($_filed) 
					FROM
						".tablename('kuyuan_vbz_activity_coupon')." as A
					LEFT JOIN
					    ".tablename('kuyuan_vbz_activity')." as B
					ON
					    A.activity_id = B.id
					WHERE
					    A.coupon_id = :coupon_id
					AND
					    A.number > A.give_number
					AND
					    A.status = 1
					AND
					    B.status = 1
					AND
					    B.start <= ".TIMESTAMP."
					 AND
					    B.end > ".TIMESTAMP."
				";
        $_sum = pdo_fetchcolumn($_sql,array('coupon_id'=>$_coupon_id));
        return $_sum ? $_sum : 0;
    }
}